﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Text;

/// <summary>
/// Summary description for Captcha
/// </summary>
public class Captcha
{
    protected StringBuilder chuoi_captcha;
    protected Bitmap Anh;

    public StringBuilder ChuoiCaptcha
    {
        get
        {
            return this.chuoi_captcha;
        }
        set
        {
        }
    }
    public Bitmap AnhBitmap
    {
        get
        {
            return this.Anh;
        }
        set
        {
        }
    }
    
    
	public Captcha()
	{
		//
		// TODO: Add constructor logic here
		//
        create_captcha(new String[] { "Arial Black" }, 200, 80, 3);
       
        
	}
    public void create_captcha(string[] Fonts,int Chieurong, int Chieucao,byte Soluong_chu)
    {
        // chuỗi để lấy các kí tự sẽ sử dụng cho captcha

    const string chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        
    // Tạo bitmap
         this.Anh = new Bitmap(Chieurong, Chieucao);
        

            // Create one graphic in this new bitmap
            using (Graphics Hinh = Graphics.FromImage(Anh))
            {
                
                
                // Create Brush Background for Graphic
                HatchBrush Conen = new HatchBrush(HatchStyle.Wave,Color.White,Color.Wheat);
                // Fill entire Graphic with selected HatchBrush
                Hinh.FillRegion(Conen, Hinh.Clip);
                // Create temporary captcha string 
                chuoi_captcha = new StringBuilder();
                // Create new random generator
                Random rand = new Random();

                for (int i = 0; i < Soluong_chu; i++)
                {
                    // Get random letter
                    string Kytu = chars[rand.Next(chars.Length)].ToString();
                    // Put it in Captcha string
                    chuoi_captcha.Append(Kytu);
                    // Get random font
                    Font Fontchu = new Font(Fonts[rand.Next(Fonts.Length)], Chieucao / 4, FontStyle.Bold | FontStyle.Italic);
                    // Get dimension of letter
                    SizeF Kichthuoc = Hinh.MeasureString(Kytu, Fontchu);
                    // Draw letter
                    Hinh.DrawString(Kytu, Fontchu, Brushes.Chocolate, i * (25) , rand.Next(20, 35));
                    // Release font cache
                    Fontchu.Dispose();
                    
                    
                }
            }
        

        
 

    }

}